package kr.saeildamil.androidstudty.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudty.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ListAddDelMulti extends Activity {
	ArrayList<String>arList;
	ArrayAdapter<String>adapter;
	ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddel);
		
		arList=new ArrayList<String>();
		arList.add("First");
		arList.add("Second");
		arList.add("Third");
		arList.add("Fourth");
		
		adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arList);
		
		list=(ListView) findViewById(R.id.ch12_listadddel_list);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		list.setAdapter(adapter);
		list.setOnItemClickListener(listener);
	}
	AdapterView.OnItemClickListener listener=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long arg3) {
			Toast.makeText(ListAddDelMulti.this, arList.get(position), Toast.LENGTH_SHORT).show();		
		}
	};
	
	public void mOnClick(View v){
		EditText et=(EditText) findViewById(R.id.ch12_listadddel_edit);
		switch (v.getId()) {
		case R.id.ch12_listadddel_addbtn:
			String str=et.getText().toString();
			et.setText("");
			arList.add(str);
			adapter.notifyDataSetChanged();
			break;
		case R.id.ch12_listadddel_del:
			SparseBooleanArray sbar=list.getCheckedItemPositions();
			Toast.makeText(this, "선택한 항목은 "+sbar.size()+"개", Toast.LENGTH_SHORT).show();
			if(sbar.size()!=0){
				for(int i=list.getCount()-1; i>0; i--){
					Toast.makeText(this, ""+sbar.get(i), Toast.LENGTH_SHORT).show();
					if(sbar.get(i)){
						arList.remove(i);
					}
				}
				list.clearChoices();
				adapter.notifyDataSetChanged();
				
			}
			break;	
			
		}
	}
	
	
}
